<?php
include_once("../dbdriver/categoryRHandle.php");
include_once("../dbdriver/categoryWHandle.php");
class categoryLogic
{
    public function getAllCatigory()
    {
        $rhandle = new categoryRHandle();
        return $rhandle->get_all_data();
    }
    public function insert_Category($array)
    {
        if(!is_array($array))
        {
            return false;
        }
        else
        {
            $whandle = new categoryWHandle();
            return $whandle->insert_data($array);
        }
    }
    public function get_category_num($array='')
    {
        $rhandle = new categoryRHandle();
        return $rhandle->get_category_num($array);
    }
    public function get_category($array='', $orderby='', $limit='')
    {
        $rhandle = new categoryRHandle();
        if($array == '')
        {
            return $rhandle->get_all_data($orderby,$limit);
        }
        else
        {
            return $rhandle->get_some_data($array, $orderby, $limit);
        }
    }
    public function update_category($array,$id)
    {
        $whandle = new categoryWHandle();
        if($id=="")
        {
            return false;
        }
        return $whandle->update_category($array,$id);
    }
    public function delete_category($id)
    {
        $whandle = new categoryWHandle();
        if($id=="")
        {
            return false;
        }
        return $whandle->delete_category($id);;
    }
    public function getCategoryById($id)
    {
        if($id=="")
        {
            return $this->getAllCatigory();
        }
        $rhandle = new categoryRHandle();
        return $rhandle->readCagoryById($id);
    }
    public function get_category_device_number($did, $cid, $createtime='')
    {
    		$rh = new categoryRHandle();
    		$array=array("did"=>$did, "cid"=>$cid);
    		if($createtime!='')
    		{
    				$array["createtime"]=$createtime;
    		}
    		return $rh->get_category_device_number($array);
    }
    public function insert_category_device($cid, $did, $createtime='')
    {
    		if($did=='' || $cid=='')
        {
        		return false;
        }
        $handle = new categoryWHandle();
        if($createtime=='')
        {
        		$createtime=date("Y-m-d H:i:s");
        }
        $array = array("did"=>"$did", "cid"=>"$cid", "createtime"=>"$createtime");
        return $handle->insert_category_device($array);
    }
    
    //please modify these functions below
    public function update_category_device_booktime($categoryId, $deviceId, $booktime)
    {
        $handle = new categoryWHandle();
        return $handle->update_category_device_booktime($categoryId, $deviceId, $booktime);
    }
    public function delete_category_device($cid, $did)
    {
        $handle = new categoryWHandle();
        $condition=array("cid"=>$cid, "did"=>$did);
        return $handle->delete_category_device($condition);
    }
    public function get_category_devices($cid)
    {
        $rhandle = new categoryRHandle();
        $array=array("cid"=>$cid);
        return $rhandle->get_category_devices($array, 'order by createtime desc');
    }
    public function searchByKeyword($keyword)
    {
    		if(strlen($keyword)<3)
    		{
    				return array();
    		}
    		$cr = new categoryRHandle();
    		return $cr->searchByKeyword($keyword, "limit 0, 30");
    }
}
?>